package com.geostar.constructionland.index.service.impl;

import com.geostar.constructionland.index.entity.RateElement;
import com.geostar.constructionland.index.mapper.RateElementMapper;
import com.geostar.constructionland.index.service.RateCalculate;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author yelll
 * @since 2020-3-16 23:48
 * sql计算接口
 **/
@Slf4j
@Component
public class RateSqlCalculate implements RateCalculate {
    @Autowired
    RateElementMapper rateElementMapper;
    @Autowired
    SqlSession sqlSession;
    @Override
    public List<Map> calRatePoint(RateElement rateElement, String startTime, String endTime, String[] areaCodes) {
        String sql = rateElement.getSql();
        /**替换区域**/
        String whereIn = "( ";
        for(String areaCode:areaCodes){
            whereIn = whereIn + "'"+areaCode+"',";
        }
        whereIn = whereIn.substring(0,whereIn.length()-1);
        whereIn = whereIn+" )";
        /**替换区域代码**/
        sql = sql.replaceAll(":areacodes",whereIn);
        /**替换时间**/
        sql = sql.replaceAll(":startTime","'"+startTime+"'");
        sql = sql.replaceAll(":endTime","'"+endTime+"'");
        List<Map> list = rateElementMapper.getResultBySql(sql);
        return list;
    }
}
